<template>
	<view class="container">
		<view class="header">
			<view class="subsection">
				<view :class=" index == subsectionCurrent?'subItem subItemCur':'subItem' " @click="subsectionChange(index)" v-for="(item, index) in subsectionList" :key="index">
					{{ item.name }}
				</view>
			</view>
		</view>
		<view class="wrapper" style="height: calc(100% - 60rpx);">
			<view class="main" >
				<scroll-view class="scroll" :scroll-y="true" :scroll-top="scrollTop" :show-scrollbar="false"
					:refresher-threshold="50" :refresher-triggered="refreshStatus" :lower-threshold="100"
					:scroll-with-animation="true" refresher-enabled="true" @refresherrefresh="onRefreshData"
					@scrolltolower="onLoadData" @scroll="onScroll">
					
					<view class="section" v-for="(item, index) in list" :key="index">
						<view class="card" v-if="item.status == 1" @click="onLink('/pages/order/venueReservation/detail/detail?id='+item.orderId)">
							<view class="cardHeader">
								<view class="ordernum">
									订单号：{{ item.orderId }}
								</view>
							</view>
							<view class="content">
								<view class="pdleft">
									<image class="hostImage" :src="item.img[0]"></image>
								</view>
								<view class="pdright">
									<view class="desc">
										场地名称：{{ item.locationName }}
									</view>
									<view  class="desc">
										预约时间：{{ item.reservationDate}} {{ item.reservationPeriod | formatPeriod}}
									</view>
									<view  class="desc">
										下单用户：{{ item.nickname }}
									</view>
									<view  class="desc">
										下单时间：{{ item.createTime }}
									</view>
									<view class="price">
										<text class="rmb">￥</text><text class="amount">{{ item.orderAmt }}</text>
									</view>
								</view>
							</view>
						</view>
						<view class="card" v-if="item.status == 2" @click="onLink('/pages/order/coursesBuy/detail/detail?id='+item.orderId)">
							<view class="cardHeader">
								<view class="ordernum">
									订单号：{{ item.orderId }}
								</view>
							</view>
							<view class="content">
								<view class="pdleft">
									<image class="hostImage" :src="item.img[0]"></image>
								</view>
								<view class="pdright">
									<view  class="desc">
										下单用户：{{ item.nickname }}
									</view>
									<view  class="desc">
										下单时间：{{ item.createTime }}
									</view>
									<view class="price">
										<text class="rmb">￥</text><text class="amount">{{ item.orderAmt }}</text>
									</view>
								</view>
							</view>
						</view>
						<view style="border-top: 1px solid #e5e5e5;padding: 20rpx 10rpx 20rpx 10rpx;text-align: right;">
							<text style="font-size: 31rpx;color: #0544ab;" @click="orderOff(item.orderId)">订单核销</text>
						</view>
					</view>
					
					
					<view class="nullData" v-if="page == 1 && list.length <=0">
						<view>
							<view>
								<image src="../../../static/image/notdata.png" class="nullImage"></image>
							</view>
							<view class="nullText">
								暂无记录
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//当前页数
				page: 1,
				//每页显示条数
				size: 10,
				//列表数据
				list: [],
				//总页数
				pages: 0,
				//监听滚动
				scrollTop: 0,
				realScrollTop: 0,
				refreshStatus: false,
				/**当前选择的状态序号**/
				subsectionCurrent: 0,
				/**订单类型分段**/
				subsectionList: [
					{type: '1',name: '场地预定'},
					{type: '2',name: '课程购买'},
				]
			}
		},
		onLoad() {
			this.fetchList();
		},
		methods: {
			// 获取订单记录
			fetchList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var params = {};
				params.current = this.page;
				params.size = this.size;
				var type = this.subsectionList[this.subsectionCurrent].type;
				if (1 == type) {
					var url = '/blade-merchant-app/merchant-app/order/order-venue';
				}else if(2 == type) {
					url = '/blade-merchant-app/merchant-app/order/order-courses';
				}
				params.venueId = uni.getStorageSync('venueId');
				//待核销订单
				params.orderStatus = 1;
				this.$request({
					url: url,
					data: params
				}).then(res => {
					const {
						records = [],
							pages
					} = res.data;
					
					records.forEach(item => {
						item.img = item.img.split(',');
					})
					
					if (this.page === 1) {
						this.list = records;
					} else {
						this.list = this.list.concat(records);
					}
					this.pages = pages || 0;
					setTimeout(() => {
						this.refreshStatus = false;
						uni.hideLoading();
					}, 300);
				})
			},
			// 监听滚动事件
			onScroll(e) {
				this.realScrollTop = e.detail.scrollTop;
			},
			// 刷新数据
			onRefreshData() {
				this.refreshStatus = true;
				this.page = 1;
				this.list = [];
				this.fetchList();
			},
			// 加载数据
			onLoadData() {
				if (this.page < this.pages) {
					this.page += 1;
					this.fetchList();
				}
			},
			//状态栏切换事件
			subsectionChange(e){
				this.subsectionCurrent = e;
				this.onRefreshData();
			},
			//订单核销
			orderOff(orderId) {
				var that = this;
				uni.showModal({
				   title: '订单核销操作',
				   content: '是否确认核销订单，核销后不可恢复？',
				   showCancel: true,
				   success: function (res) {
					   if (res.confirm) {
							uni.showLoading({
								title: '核销中',
								mask: true
							})
							that.$request({
								url: `/blade-merchant-app/merchant-app/order/order-off`,
								type: 'POST',
								data: {
									orderId: orderId
								}
							}).then(res => {
								setTimeout(()=>{
									uni.hideLoading();
									that.fetchList();
								},800)
							}).catch(err =>{
								uni.hideLoading();
							})
					   }
				   }
				});
			},
			onLink(url) {
				uni.navigateTo({ url: url })
			}
		},
		filters: {
			formatPeriod(value) {
				if (value) {
					const list = value?.split('-');
					return `${list[0].substring(0, 5)}-${list[1].substring(0, 5)}`
				}
				return value;
			}
		},
	}
</script>


<style lang="scss">
	page {
		height: 100%;
		overflow: hidden;
		background: #f1f1f1;

		.container {
			height: 100%;
			
			.header {
				display: flex;
				flex-direction: row;
				padding: 20rpx 0;
				background-color: #ffffff;
				z-index: 1;
				height: 70rpx;
			
				.subsection {
					display: flex;
					background: #ffffff; padding: 0rpx 0rpx;
					width: 100%;
					
					.subItem {
						width: 50%;
						font-size: 33rpx;
						text-align: center;
					}
					
					.subItemCur {
						font-weight: 600;
						position: relative;
						padding-bottom:10rpx;
					}
					
					.subItemCur::after {
						position: absolute;
						content: ' ';
						width: 20%;
						height: 6rpx;
						bottom: 0px;
						background: red;
						left: 40%;
					}
					
				}
			}

			.wrapper {
				display: flex;
				flex-direction: column;
				height: 100%;

				.main {
					flex: 1;
					height: 100%;

					.scroll {
						height: 100%;

						.section {
							padding: 30rpx 30rpx 20rpx 30rpx;
							background: #ffffff;
							margin-bottom: 20rpx;
							margin-top: 20rpx;

							.card {
								font-size: 30rpx;
								
								.cardHeader {
									display: flex;padding-bottom: 20rpx;
									
									.ordernum {
										width: 100%;
									}
									.orderstatus {
										width: 150rpx;text-align: right;
									}
								}
								
								.content {
									width: 100%;
									display: flex;
									
									.pdleft {
										padding-right:30rpx;
										
										.hostImage {
											width: 200rpx;
											height: 200rpx;
											border-radius: 10rpx;
										}
									}
									
									.pdright {
										
										.title {
											font-size: 35rpx;font-weight: 600;
										}
										.desc {
											font-size: 30rpx;color: #666666;line-height: 50rpx;
										}
										.price {
											color: red;
											.rmb {
												font-size: 30rpx;
											}
											.amount {
												font-size: 33rpx;
											}
										}
									}
									
									
								}
								
							}

							
						}
					}
				}
			}
		}
		
		.nullData {
			height: 100%;
			display: flex;
			-webkit-box-pack: center;
			justify-content: center;
			-webkit-box-align: center;
			align-items: center;
			
			.nullImage {
				width: 120px;height: 120px;
			}
			.nullText {
				color:#666666;font-size:29rpx;padding-top:15rpx;text-align: center;
			}
			
		}
	}
	
</style>
